<?php
namespace plugins\department\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\department\model\DepartmentModel;
use plugins\CommonUntil;
use think\Db;

/**
 * 部门
 */
class AdminIndexController extends PluginAdminBaseController
{

    /**
     * 部门列表
     */
    public function index()
    {
        return $this->fetch();
    }
    public function add_page()
    {
        $param = $this->request->param();
        $this->assign('cri_code', $param['cri_code']);
        return $this->fetch();
    }
    public function edit_page()
    {
        $param = $this->request->param();
        $this->assign('id', $param['id']);
        return $this->fetch();
    }

    /**
     * 获取部门树形
     */
    public function getDepartmentTree(){
        $param = $this->request->param();
        $departmentModel = new DepartmentModel();
        $id = '';
        if(!empty($param['id'])){
            //$department = $departmentModel->where('id',$param['id'])->find();
//            $id = $department['parent_id'];
            $id = $param['id'];
        }

        $all_list = $departmentModel->where('status',1)->select();
        $list = $departmentModel->arrayTree($all_list,0,$id);
        return api_json_echo(true, '查询成功',$list);
    }

    /**
     * 部门分页列表
     */
    public function getList(){
        $param = $this->request->param();
        $departmentModel = new DepartmentModel();
        $pageSize = '10';
        if(!empty($param['pageSize'])){
            $pageSize = $param['pageSize'];
        }
        $where['status'] = 1;
        if(!empty($param['name'])){
            $where['name'] = ['like','%'.$param['name'].'%'];
        }
        $list = $departmentModel->where($where)->paginates($pageSize)->each(function ($item) use ($departmentModel) {
            return $departmentModel->getDepartmentDetail($item);
        });
        return api_json_echo(true, '查询成功',$list);
    }

    /**
     * 获取部门详情
     */
    public function getDepartmentDetail(){
        $param = $this->request->param();
        $departmentModel = new DepartmentModel();
        if(empty($param['id'])){
            return api_json_echo(false, '传参错误');
        }
        $info = $departmentModel->where('id', $param['id'])->find();
        if($info['gis_status'] == 1){
            $info['gis_status'] = true;
        }else{
            $info['gis_status'] = false;
        }
        return api_json_echo(true, '查询成功',$info);
    }


    /**
     * 添加部门
     */
    public function add()
    {
        $param = $this->request->param();
        $departmentModel = new DepartmentModel();
        if (empty($param['address_id']) || empty($param['parent_id']) || empty($param['name'])) {
            return api_json_echo(false, '请填写完资料');
        }
        //获取数组最后一位
        if(is_array($param['address_id'])){
            $address_id = end($param['address_id']);
            $param['address_id'] = $address_id;
        }
        $parent_id = end($param['parent_id']);
        $param['parent_id'] = $parent_id;
        //上级信息
        $fa_data = $departmentModel->where('id', $param['parent_id'])->find();
        if (empty($fa_data)) {
            return api_json_echo(false, '查无上级信息');
        }
        $NO = $departmentModel->column('department_NO');
        $param['department_NO'] = max($NO) + 5;
        $param['create_uid'] = cmf_get_current_admin_id();
        $param['add_time'] =  time(); //创建时间
        $param['edit_time'] =  time(); //修改时间
        if($param['gis_status'] == 'true'){
            $param['gis_status'] = 1;
        }else{
            $param['gis_status'] = 2;
        }
        $re = $departmentModel->strict(false)->insert($param);
        if (empty($re)) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }

    /**
     * 编辑部门
     */
    public function edit(){
        $param = $this->request->param();
        $departmentModel = new DepartmentModel();
        if (empty($param['id']) || empty($param['address_id']) || empty($param['parent_id']) || empty($param['name'])) {
            return api_json_echo(false, '请填写完资料');
        }
        if(is_array($param['address_id'])){
            $address_id = end($param['address_id']);
            $param['address_id'] = $address_id;
        }
        if(is_array($param['parent_id'])){
            $parent_id = end($param['parent_id']);
            $param['parent_id'] = $parent_id;
        }
        //上级信息
        $fa_data = $departmentModel->where('id', $param['parent_id'])->find();
        if (empty($fa_data)) {
            return api_json_echo(false, '查无上级信息');
        }
        $param['edit_time'] = time(); //修改时间
        if($param['gis_status']== 'true'){
            $param['gis_status'] = 1;
        }else{
            $param['gis_status'] = 2;
        }
        $re = $departmentModel->strict(false)->update($param);
        if (empty($re)) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }

    /**
     * 删除
     */
    public function del()
    {
        $param = $this->request->param();
        if (empty($param['id'])) {
            return api_json_echo(false, '传值错误');
        }
        $departmentModel = new DepartmentModel();
        $id = $param['id'];
        //查询有无下级
        $has_count = $departmentModel->where('parent_id', $id)->count();
        if($has_count>0){
            return api_json_echo(false, '请先删除关联的下级部门');
        }
        $re = $departmentModel->where('id', $id)->delete();
        if (empty($re)) {
            return api_json_echo(false, '删除失败');
        }
        return api_json_echo(true, '删除成功');
    }

    /**
     * 导出excel
     */
    public function exportExcel()
    {
        $commonUntil = new CommonUntil();

        $departmentModel = new DepartmentModel();
        $where['status'] = 1;
        $list = $departmentModel->where($where)->select()->each(function ($item) use ($departmentModel) {
            return $departmentModel->getDepartmentDetail($item);
        });
        $expDataList = [];
        foreach ($list as $item){
            $user_list = [];
            $user_list[] = $item['name'];
            $user_list[] = $item['address_name'];
            $user_list[] = $item['contacts_name'];
            $user_list[] = $item['contacts_phone'];
            $user_list[] = $item['contacts_job'];
            $user_list[] = $item['department_num'];
            $user_list[] = $item['parent_name'];
            $user_list[] = $item['remark'];
            $expDataList[] = $user_list;
        }
        $expTitle = date('Y-m-d H:i').'-部门导出';
        $expHeadArr = ['部门名称','所属地区','负责人姓名','负责人联系方式','职位','部门人数','上级部门','备注'];
        $filePath = $commonUntil->exportExcelUntil($expTitle,$expHeadArr,$expDataList);
        return api_json_echo(true, '导出成功', zy_domain() . '/upload/' . $filePath);
    }
}
